package com.jiutian.cmsskitdev.controller;

import com.jiutian.cmsskitdev.config.ControllerStage;
import com.jiutian.cmsskitdev.service.Impl.LoginServiceImpl;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class LoginController implements ControllerStage, Initializable {


    StageController myController;

    @FXML
    private TextField usernameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private Label messageLabel;

    @FXML
    private Button loginButton;

    @FXML
    private VBox loginVBox;

    @FXML
    private void handleLogin(ActionEvent event) throws IOException {
        String username = usernameField.getText();
        String password = passwordField.getText();
        LoginServiceImpl loginService = new LoginServiceImpl();
        Boolean b = loginService.login(username, password);
        if (b){
            Parent homeRoot = FXMLLoader.load(getClass().getResource("/com/jiutian/cmsskitdev/DeviceManage.fxml"));
            Scene homeScene = new Scene(homeRoot);
            Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            stage.setScene(homeScene);
        }
    }

    @FXML
    private void handleReset() {
        usernameField.clear();
        passwordField.clear();
    }

    @Override
    public void setStageController(StageController stagecontroller) {
        this.myController = stagecontroller;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }
}
